package wsepForum.ui.console;

import java.util.Vector;

import wsepForum.common.elements.Message;
import wsepForum.common.elements.Section;
import wsepForum.common.elements.SectionHeader;
import wsepForum.common.elements.Topic;
import wsepForum.common.elements.TopicHeader;
import wsepForum.domain.Session;

public class ConsoleUIReplyGenerator {
	public ConsoleUIReplyGenerator() {}
	
	/**
	 * 
	 * @param session
	 * @return
	 */
	public String lsReplyGen(Session session) {
		try {
			String reply = "Current user location: "+ session.getUiState().getPath()+"\n\r";
			if (session.getUiState().isSection()) {
				Section sec = session.getHandler().getSection(session.getUiState().getCurrentElementID());
				reply+="******************\n\r" +
					   "** SUB-SECTIONS **\n\r" +
					   "******************\n\r";
				Vector<SectionHeader> secVec = sec.getAllSectionHeaders();
				for (int i=0; i<secVec.size(); i++) {
					reply+=i+1+"."+secVec.elementAt(i).getHeadLine()+"\n\r";
				}
				reply+="******************\n\r" +
					   "**    TOPICS    **\n\r" +
				       "******************\n\r";
				Vector<TopicHeader> topVec = sec.getAllTopicHeaders();
				for (int i=0; i<topVec.size(); i++) {
					reply+=i+1+"."+topVec.elementAt(i).getHeadLine()+"\n\r";
				}
			}
			else {
				Topic top = session.getHandler().getTopic(session.getUiState().getCurrentElementID());
				reply+="******************\n\r" +
				       "**   MESSAGES   **\n\r" +
				       "******************\n\r";
				Vector<Message> mesVec = top.getAllMessages();
				Message currentMes;
				for (int i=0; i<mesVec.size(); i++) {
					currentMes = mesVec.elementAt(i);
					reply+="Message Number: "+(i+1)+"\n\r" +
						   "Message From: "+currentMes.getUserName()+"\n\r" +
						   "Subject: "+currentMes.getHeadLine()+"\n\r" +
						   "Message: "+currentMes.getBody()+"\n\r" +
						   "*********************************\n\r";
				}
			}
			return reply;
		} catch (Exception e) {
			this.returnToRoot(session);
			return "[FAIL: Error in data-base. Returned to root section]";
		}
	}
	
	/**
	 * 
	 * @param topicNum
	 * @param session
	 * @return
	 */
	public String cdtReplyGen(int topicNum, Session session) {
		if (!session.getUiState().isSection()) {
			return "[FAIL: There are no topics in a topic]";
		}
		else {
			try {
				Vector<TopicHeader> topVec = session.getHandler().getSection(session.getUiState().getCurrentElementID()).getAllTopicHeaders();
				if ((topicNum<1) || (topicNum>topVec.size())) {
					return "[FAIL: No such topic exists]";
				}
				else {
					session.getUiState().setCurrentElementID(topVec.elementAt(topicNum-1).getElementID());
					session.getUiState().setIsSection(false);
					session.getUiState().setPath(session.getUiState().getPath()+"\\"+topVec.elementAt(topicNum-1).getHeadLine());
					return this.lsReplyGen(session);
				}
			} catch (Exception e) {
				this.returnToRoot(session);
				return "[FAIL: Error in data-base. Returned to root section]";
			}
		}
		
	}
	
	public String cdsReplyGen(int sectionNum, Session session) {
		if (!session.getUiState().isSection()) {
			return "[FAIL: There are no sections in a topic]";
		}
		else {
			try {
				Vector<SectionHeader> secVec = session.getHandler().getSection(session.getUiState().getCurrentElementID()).getAllSectionHeaders();
				if ((sectionNum<1) || (sectionNum>secVec.size())) {
					return "[FAIL: No such section exists]";
				}
				else {
					session.getUiState().setCurrentElementID(secVec.elementAt(sectionNum-1).getElementID());
					session.getUiState().setIsSection(true);
					session.getUiState().setPath(session.getUiState().getPath()+"\\"+secVec.elementAt(sectionNum-1).getHeadLine());
					return this.lsReplyGen(session);
				}
			} catch (Exception e) {
				this.returnToRoot(session);
				return "[FAIL: Error in data-base. Returned to root section]";
			}
		}
	}  
	public String helpReplyGen() {
		String reply = 
			"****************************\n\r" +
			"** COMMANDS DOCUMENTATION **\n\r" +
			"****************************\n\r" +
			"register [userName] [password]\n\r" +
			"	- registers\n\r" +
			"login [userName] [password]\n\r" +
			"	- login\n\r" +
			"loguot\n\r" +
			"	- logout\n\r" +
			"ls\n\r" +
			"	- lists the contents of the current element the user is in\n\r" +
			"cds [section-number]\n\r" +
			"	- enters the selected section, type ls to see numbers\n\r" +
			"cdt [topic-number]\n\r" +
			"	- enters the selected topic, type ls to see the numbers\n\r" +
			"cd..\n\r" +
			"	- exits the current topic\\section\n\r" +
			"addsec [section-name]\n\r" +
			"	- creates a new section in the current section\n\r" +
			"addtop [>headline] [>body]\n\r" +
			"	- creates a new topic in the current section\n\r" +
			"addmes [>headline] [>body]\n\r" +
			"	- adds a new message to the current topic\n\r" +
			"reply [>message-number] [>headline] [>body]\n\r" +
			"	- replies a message in the current topic, type ls to see the numbers\n\r" +
			"edit [>message-number] [>headline] [>body]\n\r" +
			"	- edits a message in the current topic, type ls to see the numbers\n\r" +
			"delsec [section-number]\n\r" +
			"	- deletes a section in the current section, type ls to see the numbers\n\r" +
			"delmes [message-number]\n\r" +
			"	- deletes a message in the current topic, type ls to see the numbers\n\r" +
			"addmod [userName]\n\r" +
			"	- add moderator privilages to user in the current forum\n\r" +
			"delmod [userName]\n\r" +
			"	- removes moderator privilages from user in the current forum\n\r" +
			"deluser [userName]\n\r" +
			"	- removes a user from the system\n\r";
		return reply;
	}
	
	private void returnToRoot(Session session) {
		session.getUiState().setCurrentElementID(0);
		session.getUiState().setIsSection(true);
		session.getUiState().setPath("root");
	}

	

	
}
